import logging
import os
from logging.handlers import RotatingFileHandler

src_path = os.path.dirname(os.path.abspath(__file__))


class BasicConfig(object):
    log_path = os.path.abspath(os.path.join(os.path.dirname(src_path), 'log', 'file_manage.log'))
    log_size = 1024 * 1024 * 10
    log_num = 30
    SECRET_KEY = 'file_manage'


class MongoDbConfig:
    host = 'localhost'
    port = 27017


class DefaultLogger(object):

    @classmethod
    def get_logger(cls):
        log_format = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s')
        _logger = logging.getLogger('file_manage')
        _logger.setLevel(logging.DEBUG)  # 日志等级小于 level 会被忽略

        rotating_file_handler = RotatingFileHandler(BasicConfig.log_path, maxBytes=BasicConfig.log_size,
                                                    backupCount=BasicConfig.log_num)
        rotating_file_handler.setFormatter(log_format)
        _logger.addHandler(rotating_file_handler)
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(log_format)
        _logger.addHandler(stream_handler)
        return _logger


logger = DefaultLogger.get_logger()
